package org.xurui.sxl.protocol.command;

import org.jetlinks.core.message.CommonDeviceMessage;
import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.codec.EncodedMessage;
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
import org.xurui.sxl.protocol.utils.BinaryUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface CmdCodec {
    //是否支持命令
    public boolean supportsCmd(String cmd);

    //
    public boolean supportsRelayCmd();


    //消息解码
    default public List<DeviceMessage> parsePayload(String payload, FromDeviceMessageContext context){return null;};

    //消息编码
    default public EncodedMessage encodeMessage(CommonDeviceMessage message,String zzs){return null;};

    default List<DeviceMessage> genDeviceMessage(DeviceMessage... deviceMessages){
        return Arrays.asList(deviceMessages);
    }

    default Map<String,Object> parseTrackerStatus(String trackerStatusHex){
        Map<String,Object> trackerstatus = new HashMap<>();
        String trackerStatusBinaryStr = BinaryUtils.hexToBin(trackerStatusHex);
        trackerstatus.put("m2",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,1,1));
        trackerstatus.put("m1",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,1,5));
        trackerstatus.put("gps",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,2,1));
        trackerstatus.put("m3",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,2,5));
        trackerstatus.put("jz",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,3,1));
        trackerstatus.put("dw",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,3,5));
        trackerstatus.put("wl1",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,0));
        trackerstatus.put("cd",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,1));
        trackerstatus.put("kq",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,2));
        trackerstatus.put("wl1sz",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,3));
        trackerstatus.put("mdr",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,4));
        trackerstatus.put("wifi",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,5));
        trackerstatus.put("mdrkg",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,6));
        trackerstatus.put("mdrszqk",BinaryUtils.parseTrackerStatus(trackerStatusBinaryStr,4,7));
        return trackerstatus;
    }
    default String getZzs( FromDeviceMessageContext context,String deviceId){
        return "TP";
    }
}
